package com.xmy.cultivate.controller;
import com.xmy.cultivate.entity.Admin;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.enums.SourseLeveEnum;
import com.xmy.cultivate.enums.StatusStaffEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BaseController {

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Long companyId = 1565239613641981952L;
    protected Long adminId;
    protected String companyName;
    protected Integer isSuper;
    protected Admin adminModel;

    protected Staff staffInfo;

    protected StatusStaffEnum statusStaffEnum = StatusStaffEnum.NO;

    protected SourseLeveEnum sourseLeveEnum;

    protected Integer sourseLeve;

    //管辖校区
    protected List<Long> baseSchoolIdList = new ArrayList<>();
    protected String baseSchoolIdStr = "";

    //protected Claims claims;

    //使用jwt方式获取
//    @ModelAttribute
//    public void setResAnReq(HttpServletRequest request,HttpServletResponse response) {
//        this.request = request;
//        this.response = response;
//
//        Object obj = request.getAttribute("user_claims");
//
//        if(obj != null) {
//            this.claims = (Claims) obj;
//            this.companyId = (String)claims.get("companyId");
//            this.companyName = (String)claims.get("companyName");
//        }
//    }

    //使用shiro获取
    @ModelAttribute
    public void setResAnReq(HttpServletRequest request,HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.companyId = 1565239613641981952L;

        //获取session中的安全数据
        Subject subject = SecurityUtils.getSubject();
        //1.subject获取所有的安全数据集合
        PrincipalCollection principals = subject.getPrincipals();
        if(principals != null && !principals.isEmpty()){
            //2.获取安全数据
            Admin admin = (Admin) principals.getPrimaryPrincipal();
            this.adminModel = admin;
            this.adminId = admin.getId();
            this.baseSchoolIdList = admin.getSchoolIdList();
            this.baseSchoolIdStr = String.join(",", this.baseSchoolIdList.stream().map(String::valueOf).collect(Collectors.toList()));
            if(this.baseSchoolIdStr.isEmpty()){
                this.baseSchoolIdStr = "0";
            }
            this.isSuper = admin.getIsSuper();
            if (admin.getIsSuper().equals(1)){
                statusStaffEnum = StatusStaffEnum.IN;
                sourseLeveEnum = SourseLeveEnum.ALL;
                this.sourseLeve = 1;
            }else {
                if (this.staffInfo != null){
                    statusStaffEnum = this.staffInfo.getStatus();
                }
                sourseLeveEnum = admin.getSourseLeve();
                if (this.sourseLeveEnum.equals(SourseLeveEnum.ONESELF)){
                    this.sourseLeve =2;
                }else {
                    this.sourseLeve = 1;
                }
            }

            //System.out.println();
            //System.out.print("admin");

            //System.out.print( this.adminModel);
            //System.out.println();

            //System.out.print(admin);
            /*ProfileResult result = (ProfileResult)principals.getPrimaryPrincipal();
            this.companyId = result.getCompanyId();
            this.companyName = result.getCompany();
            this.userId = result.getUserId();
            System.out.println(companyId);*/
        }
    }

}
